package org.example.medical.controller;

import org.example.medical.entity.Disease;
import org.example.medical.service.DiseaseService;
import org.example.medical.utils.ApiResult;
import org.example.medical.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/diseases")
public class DiseaseApiController {
    @Autowired
    private DiseaseService diseaseService;

    // 1. 分页查询接口
    @GetMapping
    public ApiResult<PageResult<Disease>> listDiseases(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String category,
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size
    ) {
        List<Disease> records = diseaseService.getDiseases(name, category, page, size);
        Long total = (long) records.size();
        return ApiResult.success(new PageResult<>(records, total, page, size));
    }

    // 2. 新增疾病分类记录
    @PostMapping
    public ApiResult<?> addDisease(@RequestBody Disease disease) {
        diseaseService.addDisease(disease);
        return ApiResult.success(null, "Disease record added successfully");
    }

    // 3. 更新疾病分类记录
    @PutMapping("/{diseaseId}")
    public ApiResult<?> updateDisease(
            @PathVariable Integer diseaseId,
            @RequestBody Disease disease
    ) {
        disease.setDiseaseId(diseaseId);
        diseaseService.updateDisease(disease);
        return ApiResult.success(null, "Disease record updated successfully");
    }

    // 4. 软删除疾病分类记录
    @DeleteMapping("/{diseaseId}")
    public ApiResult<?> softDeleteDisease(@PathVariable Integer diseaseId) {
        diseaseService.softDeleteDisease(diseaseId);
        return ApiResult.success(null, "Disease record deleted successfully");
    }

    // 5. 批量软删除疾病分类记录
    @PostMapping("/batchDelete")
    public ApiResult<?> batchSoftDeleteDiseases(@RequestBody List<Integer> diseaseIds) {
        diseaseService.batchSoftDeleteDiseases(diseaseIds);
        return ApiResult.success(null, "Disease records deleted successfully");
    }
}